import 'package:flutter_wgtool/OSNet/Core/OSNetAdapter.dart';
import 'package:flutter_wgtool/OSNet/Core/OSNetDioAdapter.dart';
import 'package:flutter_wgtool/OSNet/Core/OSNetError.dart';
import 'package:flutter_wgtool/OSNet/Request/BaseRequest.dart';

class OSNet {
  // OSNet._();
  //
  // static OSNet? _instance;
  //
  // static final OSNet getInstance() {
  //   if (_instance == null) {
  //     _instance = OSNet._();
  //   }
  //   return _instance!;
  // }
  OSNet._init();
  static final OSNet getInstance = OSNet._init();




  Future fire(BaseRequest request) async {
    OSNetResponse response;
    var error;
    try {
      response = await send(request);
    } on OSNetError catch (e) {
      error = e;
      response = e.data;
      printLog(e.message);
    }

    var result = response.data;
    printLog(result);

    var status = response.statusCode;
    switch (status) {
      case 200:
        return result;
        break;
      case 401:
        throw OSNetErrorNeedLogin();
        break;
      case 403:
        throw OSNetErrorNeedAuth(result.toString(), data: result);
        break;
      default:
        printLog(result.toString());
        throw OSNetError(status ?? -1, result.toString(), data: result);
        break;
    }
  }

  Future<OSNetResponse<T>> send<T>(BaseRequest request) async {
    /// 使用Dio发送请求
    OSNetAdapter adapter = OSNetDioAdapter();
    return adapter.send(request);
  }

  void printLog(log) {
    print('OSNet:' + log.toString());
  }
}
